package com.hdu.mvcInterceptor;

import com.hdu.context.FullLinkContext;
import com.hdu.context.FullLinkContextHolder;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

import static com.hdu.constant.FullLinkConstant.FULL_LINK_STRESS_HEADER;
import static com.hdu.constant.FullLinkConstant.IS_FULL_LINK_STRESS;

public class FullLinkMvcInterceptor implements WebRequestInterceptor {


    @Override
    public void preHandle(WebRequest webRequest) {
        // 防止tomcat线程池线程复用问题
        FullLinkContextHolder.clear();
        String header = webRequest.getHeader(FULL_LINK_STRESS_HEADER);
        if (IS_FULL_LINK_STRESS.equals(header)) {
            FullLinkContextHolder.markFullLinkStress(FullLinkContext.of());
        }
    }

    @Override
    public void postHandle(WebRequest webRequest, ModelMap modelMap) {
        FullLinkContextHolder.clear();
    }

    @Override
    public void afterCompletion(WebRequest webRequest, Exception e) {
        FullLinkContextHolder.clear();
    }
}
